package com.vegetable.modules.controller.stock;

import com.alibaba.fastjson.JSON;
import com.vegetable.common.annotation.SysLog;
import com.vegetable.common.utils.PageUtils;
import com.vegetable.common.utils.R;
import com.vegetable.modules.controller.AbstractController;
import com.vegetable.modules.entity.stock.StockAlertRecord;
import com.vegetable.modules.entity.stock.po.StockAlertRecordQueryPO;
import com.vegetable.modules.service.stock.IStockAlertRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 库存预警记录 前端控制器
 * </p>
 *
 * @author wangruijie
 * @since 2024-09-09
 */
@Slf4j
@RestController
@RequestMapping("/stockAlertRecord")
@AllArgsConstructor
@Api(value = "库存预警记录", tags = "库存预警记录")
public class StockAlertRecordController extends AbstractController {

    private IStockAlertRecordService stockAlertRecordService;

    /**
     * 库存预警记录列表
     */
    @PostMapping("/list")
    @ApiOperation(value = "分页查询库存预警记录列表", notes = "分页查询库存预警记录列表")
    public R list(@RequestBody StockAlertRecordQueryPO po) {
        log.info("查询库存预警记录列表：{}", JSON.toJSONString(po));
        PageUtils page = stockAlertRecordService.queryPage(po);
        return R.data(page);
    }

    /**
     * 库存预警记录详情
     */
    @GetMapping("/info/{id}")
    @ApiOperation(value = "查询库存预警记录详情", notes = "查询库存预警记录详情")
    public R info(@PathVariable("id") Long id) {
        StockAlertRecord record = stockAlertRecordService.getById(id);
        return R.data(record);
    }

    /**
     * 处理预警记录
     */
    @SysLog(value = "处理库存预警记录")
    @PostMapping("/handle")
    @ApiOperation(value = "处理库存预警记录", notes = "处理库存预警记录")
    public R handle(@RequestParam Long recordId, 
                   @RequestParam Integer alertStatus,
                   @RequestParam(required = false) String handleRemark) {
        log.info("处理库存预警记录，ID：{}，状态：{}，备注：{}", recordId, alertStatus, handleRemark);
        
        if (alertStatus != 1 && alertStatus != 2) {
            return R.fail("预警状态参数错误，只能设置为1(已处理)或2(已忽略)");
        }
        
        boolean result = stockAlertRecordService.handleAlertRecord(recordId, getUserId(), handleRemark, alertStatus);
        return R.status(result);
    }

    /**
     * 批量处理预警记录
     */
    @SysLog(value = "批量处理库存预警记录")
    @PostMapping("/batchHandle")
    @ApiOperation(value = "批量处理库存预警记录", notes = "批量处理库存预警记录")
    public R batchHandle(@RequestParam List<Long> recordIds,
                        @RequestParam Integer alertStatus,
                        @RequestParam(required = false) String handleRemark) {
        log.info("批量处理库存预警记录，数量：{}，状态：{}，备注：{}", recordIds.size(), alertStatus, handleRemark);
        
        if (alertStatus != 1 && alertStatus != 2) {
            return R.fail("预警状态参数错误，只能设置为1(已处理)或2(已忽略)");
        }
        
        boolean result = stockAlertRecordService.batchHandleAlertRecord(recordIds, getUserId(), handleRemark, alertStatus);
        return R.status(result);
    }

    /**
     * 查询未处理的预警记录数量
     */
    @GetMapping("/countUnhandled")
    @ApiOperation(value = "查询未处理的预警记录数量", notes = "查询未处理的预警记录数量")
    public R countUnhandled() {
        Long count = stockAlertRecordService.countUnhandledRecords();
        return R.data(count);
    }
}